<template>
    <div>
      <h1>测试websocket</h1>
      <input type="text" v-model="msg">
      <button type="button" @click="sendInfo">发送</button>
    </div>
</template>

<script>
    export default {
        name: "DemoWebSockt",
        data(){
          return{
             msg:"",
             socket:null,
          }

        },
        methods:{
           createWebSocket(){//创建webSocket
               this.socket = new WebSocket("ws://localhost:8080/demoWebSocket");

               //接收服务端发送过来消息
               this.socket.onmessage=function (msg) {
                   console.log(msg.data);
                  console.log("服务端发生来的消息是:"+msg.data);
               }

           },
          sendInfo(){ //发送消息到服务端
              this.socket.send(this.msg);
          }
        },
        mounted() {
           this.createWebSocket();
        }
    }
</script>

<style scoped>

</style>
